package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.TravelCommentMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCatalogQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;
    @Autowired
    private TravelCommentMapper travelCommentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void save(Travel travel) {
        Date date = new Date();
        //创建时间
        travel.setCreateTime(date);
        //最后更新时间
        travel.setLastUpdateTime(date);
        travelMapper.insert(travel);

        TravelContent travelContent = travel.getTravelContent();
        //设置主键
        travelContent.setId(travel.getId());
        //保存文章内容表
        contentMapper.insert(travelContent);
    }

    @Override
    public Travel getById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Travel travel) {
        //最后更新时间
        travel.setLastUpdateTime(new Date());
        travelMapper.updateByPrimaryKey(travel);

        TravelContent travelContent = travel.getTravelContent();
        //设置主键
        travelContent.setId(travel.getId());
        //保存文章内容表
        contentMapper.updateByPrimaryKey(travelContent);
    }

    @Override
    public TravelContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void changeState(Travel travel) {
        //如果是已发布的状态,就设置发布时间
        if (travel.getState() == Travel.STATE_RELEASE) {
            travel.setReleaseTime(new Date());
        }
        travelMapper.changeState(travel);
    }

    @Override
    public PageInfo selectByStrategyId(StrategyCatalogQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectByStrategyId(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List listAll() {
        return travelMapper.selectAll();
    }

    @Override
    public List<Travel> getTravelsByStrategyId(Long id) {
        return travelMapper.getByStrategyId(id);
    }

    @Override
    public List<Travel> selectByStrategyId(Long strategyId) {
        return null;
    }

    @Override
    public List selecttravelbyid(TravelQuery qo) {
        return travelMapper.selecttravelbyid(qo);
    }

    // 游记评论获取
    @Override
    public PageInfo getCommentsByTravelId(Long tid, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<TravelComment> list = travelCommentMapper.selectForList(tid, qo);
        return new PageInfo<>(list);
    }

    // 保存游记评论
    @Override
    public void saveComment(TravelComment travelComment) {
        travelComment.setCreateTime(new Date());
        User user = UserContext.getCurrentUser();
        travelComment.setUser(user);
        travelCommentMapper.insert(travelComment);
    }

}
